﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IsikFx.Project.ClientApp.Helpers;
using IsikFx.Project.CommonTypes.CRUD;
using IsikFx.Project.CommonTypes.User;
using IsikFx.Project.CommonTypes.Watchman;

namespace IsikFx.Project.ClientApp.Content
{
    /// <summary>
    /// Interaction logic for WatchmanEntryAndUpdate.xaml
    /// </summary>
    public partial class WatchmanEntryAndUpdate : UserControl
    {
        List<KeyValuePair<string, string>> comboPairs = new List<KeyValuePair<string, string>>();
        public List<User> SalesPersonelList { get { return GetSalesPersonelList(); } }
        public WatchmanEntryAndUpdate()
        {
            InitializeComponent();
        }


        private void dataWatchmen_Loaded_1(object sender, RoutedEventArgs e)
        {
            watchmanDate.SelectedDate = DateTime.Today;
            ReloadDataGrid(DateTime.Today.ToLongDateString());
        }

        private void cbSalesPersonel_Loaded_1(object sender, RoutedEventArgs e)
        {
            var cbxSalesPersonel = sender as ComboBox;
            List<string> nameList = new List<string>();
            comboPairs.RemoveAll(t => t.Key != string.Empty);
            nameList.Add("Lütfen Seçiniz");
            foreach (User user in SalesPersonelList)
            {
                comboPairs.Add(new KeyValuePair<string, string>(user.UserID, user.Name + " " + user.Surname));
                nameList.Add(user.Name + " " + user.Surname);
            }
            cbxSalesPersonel.ItemsSource = nameList;
            cbxSalesPersonel.SelectedIndex = 0;

        }

        private void btnAddWatchmanChanges_Click_1(object sender, RoutedEventArgs e)
        {

            string id = string.Empty;
            foreach (KeyValuePair<string, string> item in comboPairs)
            {
                if (item.Value == ((string)cbSalesPersonel.SelectedItem))
                {
                    id = item.Key;
                }
            }
            User userToBeAdded = new User();
            foreach (User user in SalesPersonelList)
            {
                if (user.UserID == id)
                {
                    userToBeAdded = user;
                }
            }

            List<Watchman> currentList = new List<Watchman>();
            watchmanDate.SelectedDateFormat = DatePickerFormat.Long;
            Watchman watchman = new Watchman() { WatchmanName = userToBeAdded.Name, AdminID = Convert.ToInt32(Helper.CurrentUser.UserID), UserId = Convert.ToInt32(userToBeAdded.UserID), WatchmanSurname = userToBeAdded.Surname, WatchmanDate = watchmanDate.SelectedDate.Value.ToLongDateString() };
            currentList = (dataWatchmen.ItemsSource as List<Watchman>);
            if (currentList == null)
                currentList = new List<Watchman>();
            currentList.Add(watchman);
            if (currentList != null && currentList.Count > 0)
            {
                lblEmptyData.Visibility = System.Windows.Visibility.Hidden;
                dataWatchmen.Visibility = System.Windows.Visibility.Visible;
            }
            dataWatchmen.ItemsSource = null;
            dataWatchmen.ItemsSource = currentList;
        }

        private void btnSaveWatchmanChanges_Click_1(object sender, RoutedEventArgs e)
        {
            List<Watchman> watchManList = dataWatchmen.ItemsSource as List<Watchman>;
            if (watchManList != null && watchManList.Count > 0)
            {
                CrudResponseMessage response = Helper.UpdateWatchmenTable(watchManList);
                if (!response.HasError)
                {
                    Helper.ShowMessage("Nöbetçi listesi başarıyla güncellendi.");
                }
                else
                {
                    if (!string.IsNullOrEmpty(response.ErrorMessage))
                    {
                        Helper.ShowMessage(response.ErrorMessage);
                    }
                }
            }
        }

        private List<User> GetSalesPersonelList()
        {

            IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
            UserResponseMessage salesPersonelList = client.GetUsersByRole((int)Helper.Roles.Sales);
            if (salesPersonelList.Response != null)
            {
                if (salesPersonelList.HasError)
                {
                    Helper.ShowMessage(salesPersonelList.ErrorMessage);
                }
                else
                {
                    return salesPersonelList.Response.User;
                }
            }
            return new List<User>();
        }

        private void ReloadDataGrid(string dateTime)
        {
            lblEmptyData.Visibility = System.Windows.Visibility.Hidden;
            dataWatchmen.Visibility = System.Windows.Visibility.Visible;
            dataWatchmen.ItemsSource = null;
            List<Watchman> list = (dataWatchmen.ItemsSource as List<Watchman>);
            WatchmanResponseMessage watchmanResponseMsg = Helper.GetWatchmenList(dateTime);
            list = watchmanResponseMsg.Response.WatchmanList;
            if ((list == null || list.Count == 0))
            {
                dataWatchmen.Visibility = System.Windows.Visibility.Hidden;
                lblEmptyData.Visibility = System.Windows.Visibility.Visible;
                return;
            }
            else if (list != null && list.Count > 0)
            {
                dataWatchmen.ItemsSource = list;
            }
        }
        private void watchmanDate_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e)
        {
            watchmanDate.SelectedDateFormat = DatePickerFormat.Long;
            ReloadDataGrid(watchmanDate.SelectedDate.Value.ToLongDateString());
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            List<Watchman> currentDataList = dataWatchmen.ItemsSource as List<Watchman>;
            if (currentDataList == null || currentDataList.Count == 0)
                return;
            Watchman selectedRow = ((sender as Button).DataContext as Watchman);
            currentDataList.Remove(selectedRow);
            dataWatchmen.ItemsSource = null;
            dataWatchmen.ItemsSource = currentDataList;

        }
    }
}
